package com.zlf.mvc.controller;

import com.zlf.entity.Auth;
import com.zlf.entity.Role;
import com.zlf.service.api.AdminService;
import com.zlf.service.api.AuthService;
import com.zlf.service.api.RoleService;
import com.zlf.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author ：张利锋
 * @date ：Created in 2020/11/20 13:13
 * @description： TOOD
 * @version: 1.0
 */
@Controller
public class AssignController {
    @Autowired
    AdminService adminService;
    @Autowired
    RoleService roleService;
    @Autowired
    AuthService authService;

    @RequestMapping("/assign/get/assigned/auth/id/by/role/id")
    @ResponseBody
    public ResultEntity<List<Integer>> getAuthByRoleId(Integer roleId){
     List<Integer> authList=authService.getAuthByRoleId(roleId);
     return ResultEntity.successWithData(authList);
    }

    @RequestMapping("/assign/do/role/auth")
    @ResponseBody
    public ResultEntity<String> updateAuthByRoleId(@RequestBody Map<String, List<Integer>> map){
     authService.updateAuthByRoleId(map);
     return ResultEntity.successWithoutData();
    }

    @RequestMapping("assign/get/all/auth")
    @ResponseBody
    public ResultEntity<List<Auth>> getAllAuth(){
        List<Auth> authList=authService.getAllAuth();
        return ResultEntity.successWithData(authList);
    }


    @RequestMapping("/assign/do/role/assign")
    public String doAssign(@RequestParam(value = "roleList",required = false) List<Integer> roleList,
                           @RequestParam("pageNum") Integer pageNum,
                           @RequestParam("keyword") String keyword,
                           @RequestParam("adminId") Integer adminId){
            adminService.insertAdminRoleRelationship(adminId,roleList);
            return "redirect:/admin/page?keyword=" + keyword + "&pageNum=" + pageNum;
    }

    @RequestMapping("/assign/to/role/page")
    public String toAssignPage(@RequestParam("adminId") Integer adminId, Model model){
        List<Role> assignenRole= roleService.getAssignedRole(adminId);
        List<Role> unAssignenRole= roleService.getUnAssignedRole(adminId);
        model.addAttribute("assignenRole",assignenRole);
        model.addAttribute("unAssignenRole",unAssignenRole);
        return "assign-role";
    }
}
